package com.skyline.wo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;
import com.skyline.wo.model.Video;
import com.skyline.wo.service.VideoService;

@Controller
@RequestMapping("/video")
public class VideoController {

	@Autowired
	private VideoService videoService;

//	@Value("${view.video.view}")
//	private String viewVideoView;
//	
//	@Value("${view.video.list}")
//	private String listVideoView;

	@RequestMapping(value = "/queryInfo", method = RequestMethod.GET)
	public @ResponseBody
	Video queryVideoInfo(String url) {
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null || user.getId() == 0) {
			return null;
		} else {
			Video video;
			try {
				video = videoService.queryVideoInfo(url);
				if (video != null) {
					video.setOwnerId(user.getId());
					video.setOwnerNickname(user.getNickname());
					video.setOwnerPortrait(user.getPortrait());
					WebHelper.setSessionAttribute(null, "lastQueryVideoInfo", video);
				}
			} catch (Exception e) {
				// 对出错的情况进行处理
				video = null;
			}
			return video;
		}
	}

	@RequestMapping(value = "/addVideo", method = RequestMethod.POST)
	@ResponseBody
	public long addVideo(Video modifyedVideo) {
		Video video = (Video) WebHelper.getSessionAttribute(null, "lastQueryVideoInfo");
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (video == null || user == null || user.getId() == 0) {
			return 0;
		} else {
			long id = videoService.addVideo(user.getId(), user.getPortrait(), user.getNickname(), modifyedVideo.getTitle(),
					video.getThumbnail(), modifyedVideo.getSummary(), video.getTime(), video.getSource(), video.getPageUrl(),
					video.getFlashUrl(), video.getHtmlCode());
			return id;
		}
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public ModelAndView view(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView();
		Video video = videoService.getVideoById(id);
		mav.addObject("video", video);
		mav.setViewName(ViewPaths.VIDEO_VIEW);
		return mav;
	}

	@RequestMapping(value = "/list/{ownerId}", method = RequestMethod.GET)
	public ModelAndView listByOwnerId(@PathVariable Long ownerId, Page page) {
		ModelAndView mav = new ModelAndView();
		List<Video> videos = videoService.getVideoByOwnerId(ownerId, page);
		mav.addObject("videos", videos);
		mav.addObject("page", page);
		mav.addObject("ownerId", ownerId);
		mav.setViewName(ViewPaths.VIDEO_LIST);
		return mav;
	}
}
